//
//  UserDetailViewController.m
//  FoodFamily
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "UserDetailViewController.h"
#import "StoreViewController.h"
#import "FoodAboutModle.h"

@interface UserDetailViewController ()

@end

@implementation UserDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButton)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];


    self.array = [NSMutableArray array];
    [AFNGetDataValue getDataAFN:[NSString stringWithFormat:@"http://api.meishixing.com/user/user/userbaseinfo/uid=%@&session_id=000012a3048e08151bb0a6fe0608af0f5cf2d0",self.user_id]block:^(NSMutableDictionary *dataDic) {
      
        self.dic1 = [dataDic objectForKey:@"result"];
        if (self.dic1.count != 0 ) {
            [self.HUD hide:YES];
            if (WIDTH >= 375 ) {
                self.useView = [[UserView1 alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
                self.useView.backgroundColor = [UIColor whiteColor];
                [self.view addSubview:self.useView];
                
                self.useView.contentSize =CGSizeMake(0, self.view.frame.size.height+5);
                
            }else if (WIDTH < 375){
            
                self.useView = [[UserView1 alloc]initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height + 50)];
                self.useView.backgroundColor = [UIColor whiteColor];
                [self.view addSubview:self.useView];
                
                self.useView.contentSize =CGSizeMake(0, self.view.frame.size.height+150);
            
            
            }
            
            
            
            self.useView.Mydelegate = self;
            
        }else if(self.dic1.count == 0 ){
            [self.HUD hide:YES];
            
            UIAlertView *alView = [[UIAlertView alloc]initWithTitle:nil message:@"未连接到网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alView show];
        
        
        }
        
        
        FoodCityModle *foodModel = [[FoodCityModle alloc]init];
        foodModel.user_image = [self.dic1 objectForKey:@"user_image"];
        foodModel.last_picture_list = [self.dic1 objectForKey:@"last_picture_list"];
        foodModel.name = [self.dic1 objectForKey:@"name"];
        foodModel.tobeloved = [self.dic1 objectForKey:@"tobeloved"];
        foodModel.reputation = [self.dic1 objectForKey:@"reputation"];
        foodModel.user_follow_count = [self.dic1 objectForKey:@"user_follow_count"];
        foodModel.user_fans_count = [self.dic1 objectForKey:@"user_fans_count"];
        foodModel.user_id = [self.dic1 objectForKey:@"user_id"];
        foodModel.level_title = [self.dic1 objectForKey:@"level_title"];
        self.array = [self.dic1 objectForKey:@"last_picture_list"];
     
        NSURL *url = [NSURL URLWithString:foodModel.user_image];
            [self.useView.user_image sd_setImageWithURL:url];
        
       
            self.useView.user_name.text = foodModel.name;
        self.useView.user_name.font = [UIFont systemFontOfSize:20];
            //动态确定字体的宽
            UIFont *fnt = [UIFont systemFontOfSize:20];
            CGSize size = [self.useView.user_name.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
         self.navigationItem.title = foodModel.name;
            self.useView.length = size.width;
            self.useView.level_title = [[UILabel alloc]initWithFrame:CGRectMake(self.useView.length + 5, 5, 40,20)];
            self.useView.level_title.backgroundColor = [UIColor redColor];
            self.useView.level_title.layer.masksToBounds = YES;
            self.useView.level_title.layer.cornerRadius = 10;
        self.useView.level_title.alpha = 0.8;
            [self.useView.user_name addSubview:self.useView.level_title];
            self.useView.level_title.font = [UIFont systemFontOfSize:9];
            self.useView.level_title.text = foodModel.level_title;
            self.useView.level_title.textColor= [UIColor yellowColor];
            self.useView.level_title.textAlignment = YES;
      
        
        self.useView.tobeloved.text =[NSString stringWithFormat:@"分享的美食被%@人喜欢过",foodModel.tobeloved];
        self.useView.tobeloved.font = [UIFont systemFontOfSize:18];
        self.useView.tobeloved.alpha = 0.5;
        
        NSString *str =[NSString stringWithFormat:@"%@",foodModel.reputation] ;
        [self.useView.user_tweet_count setTitle:str forState:UIControlStateNormal];
        [self.useView.user_tweet_count setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        
        NSString *str1 =[NSString stringWithFormat:@"%@",foodModel.user_follow_count] ;
        [self.useView.user_follow_count setTitle:str1 forState:UIControlStateNormal];
        [self.useView.user_follow_count setTitleColor:[UIColor  redColor] forState:UIControlStateNormal];
     
        NSString *str2 =[NSString stringWithFormat:@"%@",foodModel.user_fans_count] ;
        [self.useView.user_fans_count setTitle:str2  forState:UIControlStateNormal];
        [self.useView.user_fans_count setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        
        
        
        
        
        
        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
        for (int i = 0; i < foodModel.last_picture_list.count; i++) {
            
            
            
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0,i * 65, 60, 60)];
            imageV.backgroundColor = [UIColor whiteColor];
            [self.useView.imageV1 addSubview:imageV];
            [imageV release];
            
            
            if (i== 0) {
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame =CGRectMake(15,  i * 70, WIDTH - 25, 60);
                button.backgroundColor = [UIColor clearColor];
                [self.useView.imageV1 addSubview:button];
                 [button addTarget:self action:@selector(MyButtonAction) forControlEvents:UIControlEventTouchUpInside];
            }else if (i==1){
                UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
                button2.frame =CGRectMake(15,  i * 70, WIDTH - 25, 60);
                button2.backgroundColor = [UIColor clearColor];
                [self.useView.imageV1 addSubview:button2];
                [button2 addTarget:self action:@selector(MyButtonAction1) forControlEvents:UIControlEventTouchUpInside];
            
            }
            
           
            dic2 = [foodModel.last_picture_list objectAtIndex:i];
            
            NSURL *url = [NSURL URLWithString:[dic2 objectForKey:@"picture_url"]];
            [imageV sd_setImageWithURL:url];
            
            UILabel *food_name = [[UILabel alloc]initWithFrame:CGRectMake(70, i * 72 , 100 , 18)];
            food_name.text = [dic2 objectForKey:@"food_name"];
            food_name.font = [UIFont systemFontOfSize:19];
            [self.useView.imageV1 addSubview:food_name];
            [food_name release];
            
            
            
            UILabel *place_name = [[UILabel alloc]initWithFrame:CGRectMake(70, i * 72+20 , WIDTH - 180 , 18)];
            
            place_name.text =[NSString stringWithFormat:@"@%@",[dic2 objectForKey:@"place_name"]];
            [self.useView.imageV1 addSubview:place_name];
            place_name.font = [UIFont systemFontOfSize:17];
            place_name.alpha = 0.8;
            [place_name release];
            
            
            UILabel *publish_time = [[UILabel alloc]initWithFrame:CGRectMake(70,  i * 72+35 , WIDTH - 180 , 18)];
            publish_time.text = [dic2 objectForKey:@"publish_time"];
            [self.useView.imageV1 addSubview:publish_time];
            publish_time.alpha = 0.6;
            publish_time.font = [UIFont systemFontOfSize:14];
            [publish_time release];
            
            UILabel *want_it_total = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH - 110,  i * 72+35 , WIDTH - 180 , 18)];
        
            want_it_total.text =  [NSString stringWithFormat:@"有%@人喜欢",[dic2 objectForKey:@"want_it_total"]];
            
            want_it_total.alpha = 0.6;
            want_it_total.font = [UIFont systemFontOfSize:14];
            [self.useView.imageV1 addSubview:want_it_total];
            [want_it_total release];
            
            
            
        }
        
        
        
        
    }];

    
    

    
   self.useView.Mydelegate = self;
    
    
    self.dic = [NSMutableDictionary dictionary];
    
    
    // Do any additional setup after loading the view.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    [self.navigationController popViewControllerAnimated:YES];

}

-(void)leftButton{
    [self.navigationController popViewControllerAnimated:YES];


}

-(void)bringButton{
    StoreViewController *storeVC = [[StoreViewController alloc]init];
    [self.navigationController pushViewController:storeVC animated:YES];
    
    storeVC.dic = self.dic1;
    
   
    
    [storeVC release];
    
}
-(void)bringButton1{
    StoreViewController *storeVC = [[StoreViewController alloc]init];
    [self.navigationController pushViewController:storeVC animated:YES];
    
    storeVC.dic = self.dic1;
    
    
    [storeVC release];
}

-(void)bringButton2{

    


}
-(void)bringButton3{


}
//push到美食页面
-(void)MyButtonAction{
    
  self.dic =   [self.array objectAtIndex:0];
    FoodAboutModle *hotModle = [[FoodAboutModle alloc]init];
    hotModle.tweet_id =[self.dic objectForKey:@"tweet_id"];
    HotAndMagazineViewController *hotVC = [[HotAndMagazineViewController alloc]init];
    hotVC.hotModle = hotModle;
    [self.navigationController pushViewController:hotVC animated:YES];
    [hotVC release];

}
-(void)MyButtonAction1{
 
    self.dic =   [self.array objectAtIndex:1];
    FoodAboutModle *hotModle1 = [[FoodAboutModle alloc]init];
    hotModle1.tweet_id =[self.dic objectForKey:@"tweet_id"];
    HotAndMagazineViewController *hotVC = [[HotAndMagazineViewController alloc]init];
    hotVC.hotModle = hotModle1;
    [self.navigationController pushViewController:hotVC animated:YES];
    [hotVC release];

}
-(void)dealloc{
    [_user_id release];
    [_useView release];
    [_array release];
    [_dic release];
    [_dic1 release];
    [_HUD release];
    [super dealloc];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
